package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.GroupSegmNumDTO;
import cqrtplm.dto.query.RtSegmNumQueryDTO;
import cqrtplm.entity.RtSegmNumDO;
import cqrtplm.service.RtSegmNumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(RouteConstant.SEGMNUM+"v1")
//    public static final String SEGMNUM = "rest/SegmNum/";
public class RtSegmNumController {

    @Autowired
    private RtSegmNumService rtSegmNumService;

    @PostMapping
    public int save(@RequestBody RtSegmNumDO record) {
        try{rtSegmNumService.save(record);}catch (Exception e){
            return 2;
        }
        return 1;
    }

    @PutMapping
    public int update(@RequestBody RtSegmNumDO record) {
        try{rtSegmNumService.update(record);}catch (Exception e){
            return 2;
        }
        return 1;
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable String id) {
        return rtSegmNumService.delete(id);
    }

    @GetMapping("/{id}")
    public RtSegmNumDO getById(@PathVariable String id) {
        return rtSegmNumService.getById(id);
    }

    @PostMapping("/listPage")
    public ResponseResult<PageInfo<RtSegmNumDO>> listAll(@RequestBody RtSegmNumQueryDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<RtSegmNumDO> list = rtSegmNumService.listAll(dto);
        PageInfo<RtSegmNumDO> pageInfo =new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }

    /**
     * 段号群组绑定
     * @param dto
     * @return
     */
    @PostMapping("/binding")
    public ResponseResult binding(@RequestBody GroupSegmNumDTO dto) {
        return rtSegmNumService.biding(dto);
    }

    /**
     * 根据群组id获取段号信息
     * @param groupId
     * @return
     */
    @GetMapping("/getGroupSegm")
    public ResponseResult getGroupSegm(@RequestParam String groupId) {
        return rtSegmNumService.getGroupSegm(groupId);
    }

    /**
     * 根据段号查询当前用户是否具有搭建BOM的权限
     * @param segmNumId
     * @return
     */
    @GetMapping("/getSegmNumIdPower")
    public ResponseResult getSegmNumIdPower(@RequestParam String segmNumId) {
        return rtSegmNumService.getSegmNumIdPower(segmNumId);
    }

    /**
     * 根据段号id获取装配位置
     * @param id
     * @return
     */
//    /rest/SegmNum/v1/getPostionBySegmNum
    @GetMapping("/getPostionBySegmNum")
    public String   getPostionBySegmNum(@RequestParam String id) {
        RtSegmNumDO rtSegmNumDO = rtSegmNumService.getById(id);
        if (rtSegmNumDO != null){
            return rtSegmNumDO.getPosition();
        }
        return "";
    }
    //根据获取所有段号
    @GetMapping("/querySegmNumByCategory")
    public JSONObject querySegmNumByCategory(@RequestParam String category) {
        return rtSegmNumService.querySegmNumByCategory(category);
    }
}
